<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryParams.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="handleInput" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          @current-change="getId"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="excelClick">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employList">
          <el-table-column prop="staffPhoto" label="头像" width="80px" align="center">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="box">{{ row.username?.charAt(0) }}</span>
            </template>

          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 0">非正式</span>
              <span v-else>其他</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" size="mini" @click="look(row.id)">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteRule(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" style="height:60px">
          <el-pagination
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            layout="total, prev, pager, next"
            :total="queryParams.total"
            @current-change="changePage"
          /></el-row>
      </div>
      <importExcel :show-excel-dialog.sync="showExcelDialog" @uploadList="getEmployeeList" />
    </div>
  </div>
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { transList } from '@/utils/index'
import { getEmployeeList, exportExcelApi, deleteExcelApi } from '@/api/employee'
import FileSaver from 'file-saver'
import importExcel from '@/views/employee/components/importExcel'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      depts: [],
      queryParams: {
        departmentId: null,
        total: 0,
        page: 1,
        pagesize: 10,
        keyword: ''

      },
      employList: [],
      showExcelDialog: false
    }
  },
  created() {
    this.getEmployeeList()
    this.getDepartmentList()
  },
  methods: {
    look(id) {
      this.$router.push(`/employee/detail/${id}`)
    },
    async deleteRule(id) {
      await deleteExcelApi(id)
      this.$message.success('删除成功')
      if (this.queryParams.page >= 1 && this.employList.length === 1) {
        this.queryParams.page--
      }
      this.getEmployeeList()
    },
    handleInput() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 800)
    },
    async getDepartmentList() {
      const data = await getDepartmentList()
      const newData = transList(data)
      this.depts = newData
      this.queryParams.departmentId = this.depts[0].id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.depts[0].id)
      })
    },
    getId(data) {
      this.queryParams.departmentId = data.id
      this.queryParams.page = 1
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.queryParams)
      this.employList = rows
      this.queryParams.total = total
    },
    changePage(currentPage) {
      this.queryParams.page = currentPage
      this.getEmployeeList()
    },
    async excelClick() {
      const result = await exportExcelApi()
      FileSaver.saveAs(result, '员工列表.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
@keyframes identifier {
  100% {
    transform:  translate(20px) rotate(-360deg);
  }
}
.box {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 20px;
          height: 20px;
          border-radius: 50%;
          color: #f1990b;
          background-image: linear-gradient(#8a068d , #330693);
          box-shadow: 0 0 5px 5px #f38405;
          transform: translate(20px);
          animation: identifier infinite 3s linear;
        }
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
